package cn.wenhe9.hefengweather.logic.dao

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import cn.wenhe9.hefengweather.HeFengWeatherApplication
import cn.wenhe9.hefengweather.logic.model.Location
import cn.wenhe9.hefengweather.logic.model.Place
import com.google.gson.Gson

/**
 *@author DuJinliang
 *2021/10/2
 */
object PlaceDao {
    fun savePlace(place : Location){
        sharedPreferences().edit{
            putString("place", Gson().toJson(place))
        }
    }

    fun getSavedPlace() : Location{
        val placeJson = sharedPreferences().getString("place", "")
        return Gson().fromJson(placeJson, Location::class.java)
    }

    fun isPlaceSaved() = sharedPreferences().contains("place")

    private fun sharedPreferences() = HeFengWeatherApplication.context.getSharedPreferences("hefeng_weather", Context.MODE_PRIVATE)
}